﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcctrueWMS.Foundation.Data;
using System.IO;
using System.Xml.Linq;

namespace AcctrueWMS.Foundation.Web.Config
{
    class ConfigManager:IConfigManager
    {
        private static Dictionary<string, string> dic_config = new Dictionary<string, string>();
        public void LoadFromFile(string path)
        {
            if (File.Exists(path))
            {
                XDocument xmlCache = XDocument.Load(path);
                IEnumerable<XElement> xNode = xmlCache.Root.Elements();
                foreach (XElement xe in xNode)
                {
                    string key = xe.Attribute("key") == null ? string.Empty : xe.Attribute("key").Value;
                    string strvalue = xe.Attribute("value") == null ? string.Empty : xe.Attribute("value").Value;
                    Object val = CommandExprFactory.Eval(strvalue, Runtime.Context);
                    if (!dic_config.ContainsKey(key))
                    {
                        dic_config[key] = val.ToString();
                    }
                }
            }
        }

        public string GetConfig(string key)
        {
            if (dic_config.ContainsKey(key))
                return dic_config[key];
            return string.Empty;
        }
    }
}
